package com.lemon.kenhnhacvn;

import static android.view.Gravity.START;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import saucaca.CustomView.CustomActionbar;
import saucaca.Object.SongObject;
import saucaca.Util.AppController;
import saucaca.Util.TagName;
import saucaca.fragment.FragmentErrorConnect;
import saucaca.fragment.LocalAllMusic;
import saucaca.fragment.MenuFragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

public class BaseActivity extends FragmentActivity {
	private static final String TAG = BaseActivity.class.getSimpleName();
	public List<Fragment> mFragmentList = new ArrayList<Fragment>();
	public MenuFragment menuFragment;
	public Fragment mCurrentFragment = null;
	public static final String ID = "id";
	public static final String INDEX = "index";

	ImageView imgMenuNavigate, imgMini_album;
	TextView textTitle, textArtist, textBufferStart, textBufferEnd;
	SeekBar SeekbarBuffer;
	public ImageLoader imageLoader = AppController.getInstance().getImageLoader();
	View panel_control_mini, panel_details;
	// public static Animation AlbumAnimation;
	public static int w = 0, h = 0;
	public static android.widget.LinearLayout.LayoutParams linear_params_4;
	public static android.widget.LinearLayout.LayoutParams linear_params_3;
	public static android.widget.LinearLayout.LayoutParams linear_params_2;
	static Context context;

	public static ArrayList<SongObject> currentPlaylist = new ArrayList<SongObject>();
	public static int currentIndex;
	public static long currentDuration;
	public CustomActionbar myActionBar;

	public static String IP = "";
	public static String IS3G = "";
	public static boolean NETWORK_CONNECTED = false;

	private static String NET_3G = "1";
	private static String NET_WIFI = "0";

	DrawerLayout drawer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		init();
	}

	void init() {
		findviewbyid();
		drawer.closeDrawer(START);
		IP = getLocalIpAddress();
		if (BaseEngine.isWifiConnected(BaseActivity.this)) {
			IS3G = NET_WIFI;
			NETWORK_CONNECTED = true;
		}
		if (BaseEngine.isMobileConnected(BaseActivity.this)) {
			IS3G = NET_3G;
			NETWORK_CONNECTED = true;
		}
		myActionBar = (CustomActionbar) findViewById(R.id.main_actionbar);
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		w = (int) metrics.widthPixels;
		h = (int) metrics.heightPixels;
		initPopulateFragment();

		context = getApplicationContext();

		int w = (int) (BaseActivity.w * 0.9 / 3);
		int h = w / 2;
		linear_params_3 = new android.widget.LinearLayout.LayoutParams(w, h);
		
		linear_params_4 = new android.widget.LinearLayout.LayoutParams(w, w*3/4);
		
		linear_params_2 = new android.widget.LinearLayout.LayoutParams((int) (BaseActivity.w * 0.9 / 2), (int) (BaseActivity.w * 0.9 / 2) / 2);
		
		panel_control_mini=findViewById(R.id.panel);
	}

	public void showMenu() {
		if (drawer.isDrawerVisible(START)) {
			drawer.closeDrawer(START);
		} else {
			drawer.openDrawer(START);
		}
	}

	public void showPanelControl(boolean isShow) {
		if(isShow){
		panel_control_mini.setVisibility(View.VISIBLE);
		}else{
			panel_control_mini.setVisibility(View.GONE);	
		}
	}

	private void findviewbyid() {
		drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
		panel_control_mini = findViewById(R.id.panel);
		panel_details = findViewById(R.id.panel_details);

		imgMini_album = (ImageView) findViewById(R.id.mini_picture_control);
	}

	private void initPopulateFragment() {
		menuFragment = new MenuFragment();
		mCurrentFragment = new LocalAllMusic();
		FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		fragmentTransaction.replace(R.id.frame_menu, menuFragment, menuFragment.getClass().getName());
		fragmentTransaction.replace(R.id.frame_main, mCurrentFragment, mCurrentFragment.getClass().getName());
		fragmentTransaction.commit();
		mFragmentList.add(mCurrentFragment);
	}

	public void switchContent(String fragmentName) {
		Fragment f = null;
		boolean existed = false;
		if (mCurrentFragment != null) {
			if (!mCurrentFragment.getClass().getName().equals(fragmentName)) {
				for (int i = 0; i < mFragmentList.size(); i++) {
					if (mFragmentList.get(i).getClass().getName().equals(fragmentName)) {
						existed = true;
						f = mFragmentList.get(i);
						FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
						fragmentTransaction.hide(mCurrentFragment).show(f);
						fragmentTransaction.commit();
						mCurrentFragment = f;
						break;
					}
				}
				if (!existed) {
					f = Fragment.instantiate(getApplicationContext(), fragmentName);
					mFragmentList.add(f);
					FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
					fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, f);
					fragmentTransaction.commit();
					mCurrentFragment = f;
				}
			}
		} else {
			f = Fragment.instantiate(getApplicationContext(), fragmentName);
			mFragmentList.add(f);
			FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.add(R.id.frame_main, f);
			fragmentTransaction.commit();
		}
		drawer.closeDrawers();
	}

	public void showprevousFragment() {
		{
			Fragment f = null;
			try {
				if (mFragmentList.size() > 0)
					for (int i = 0; i < mFragmentList.size(); i++) {
						if (mFragmentList.get(i).getClass().getName().equals(mCurrentFragment.getClass().getName())) {
							if (mFragmentList.get(i - 1).getClass().getName() != FragmentErrorConnect.class.getName()) {
								f = mFragmentList.get(i - 1);
								getSupportFragmentManager().beginTransaction().hide(mCurrentFragment).show(f).commit();
								getSupportFragmentManager().beginTransaction().remove(mFragmentList.get(i)).commit();
								mFragmentList.remove(i);
							} else {
								f = mFragmentList.get(i - 2);
								getSupportFragmentManager().beginTransaction().hide(mCurrentFragment).show(f).commit();
								getSupportFragmentManager().beginTransaction().remove(mFragmentList.get(i)).commit();
								mFragmentList.remove(i);
								mFragmentList.remove(i - 1);
							}
							mCurrentFragment = f;
							break;
						}
					}
			} catch (Exception e) {
			}
		}
		drawer.closeDrawers();
	}

	@SuppressLint("Recycle")
	public void reloadFragment(Serializable data) {
		if (mFragmentList.size() > 0) {
			getSupportFragmentManager().beginTransaction().detach(mCurrentFragment).commit();
			Bundle arguments = mCurrentFragment.getArguments();
			arguments.putSerializable(ID, data);
			getSupportFragmentManager().beginTransaction().attach(mCurrentFragment).commit();
		}
		drawer.closeDrawers();
	}

	public void switchContent(String fragmentName, String data) {
		Fragment f = null;
		boolean existed = false;
		if (mCurrentFragment != null) {
			if (!mCurrentFragment.getClass().getName().equals(fragmentName)) {
				for (int i = 0; i < mFragmentList.size(); i++) {
					if (mFragmentList.get(i).getClass().getName().equals(fragmentName)) {
						existed = true;
						f = mFragmentList.get(i);
						Bundle arguments = f.getArguments();
						arguments.putString(ID, data);
						FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
						fragmentTransaction.hide(mCurrentFragment).show(f);
						fragmentTransaction.commit();
						mCurrentFragment = f;
						break;
					}
				}
				if (!existed) {
					f = Fragment.instantiate(getApplicationContext(), fragmentName);
					Bundle arguments = new Bundle();
					arguments.putString(ID, data);
					f.setArguments(arguments);
					mFragmentList.add(f);
					FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
					fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, f);
					fragmentTransaction.commit();
					mCurrentFragment = f;
				}
			}
		} else {
			f = Fragment.instantiate(getApplicationContext(), fragmentName);
			Bundle arguments = new Bundle();
			arguments.putString(ID, data);
			f.setArguments(arguments);
			mFragmentList.add(f);
			FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.add(R.id.frame_main, f);
			fragmentTransaction.commit();
		}
		drawer.closeDrawers();
	}

	public void menuselect(String fragmentName) {
		Fragment f = null;
		try {
			if (mCurrentFragment != null) {
				f = Fragment.instantiate(getApplicationContext(), fragmentName);
				mFragmentList.add(f);
				FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
				fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, f);
				fragmentTransaction.commit();
				mCurrentFragment = f;
				if (mFragmentList.size() > 1)
					for (int i = 0; i < mFragmentList.size() - 1; i++) {
						getSupportFragmentManager().beginTransaction().remove(mFragmentList.get(i)).commit();
						mFragmentList.remove(i);
					}
			} else {
				f = Fragment.instantiate(getApplicationContext(), fragmentName);
				mFragmentList.add(f);
				FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
				fragmentTransaction.add(R.id.frame_main, f);
				fragmentTransaction.commit();
			}
		} catch (Exception e) {
		}
		drawer.closeDrawers();
	}

	public void switchContent(Fragment fragment) {
		if (mCurrentFragment != null) {
			if (!mCurrentFragment.getClass().getName().equals(fragment.getClass().getName())) {
				mFragmentList.add(fragment);
				FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
				fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, fragment);
				fragmentTransaction.commit();
				mCurrentFragment = fragment;
			}
		} else {
			mFragmentList.add(fragment);
			FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.add(R.id.frame_main, fragment);
			fragmentTransaction.commit();
		}
		drawer.closeDrawers();
	}

	public void switchContent2(Fragment fragment) {
		mFragmentList.add(fragment);
		FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, fragment);
		fragmentTransaction.commit();
		mCurrentFragment = fragment;
		drawer.closeDrawers();
	}

	public void switchContent(String fragmentName, Serializable data) {
		Fragment f = null;
		boolean existed = false;
		if (mCurrentFragment != null) {
			if (!mCurrentFragment.getClass().getName().equals(fragmentName)) {

				for (int i = 0; i < mFragmentList.size(); i++) {
					if (mFragmentList.get(i).getClass().getName().equals(fragmentName)) {
						existed = true;
						f = mFragmentList.get(i);
						Bundle arguments = f.getArguments();
						arguments.putSerializable(ID, data);
						FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
						fragmentTransaction.hide(mCurrentFragment).show(f);
						fragmentTransaction.commit();
						mCurrentFragment = f;
						break;
					}
				}
				if (!existed) {
					f = Fragment.instantiate(getApplicationContext(), fragmentName);
					Bundle arguments = new Bundle();
					arguments.putSerializable(ID, data);
					f.setArguments(arguments);
					mFragmentList.add(f);
					FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
					fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, f);
					fragmentTransaction.commit();
					mCurrentFragment = f;
				}
			}
		} else {
			f = Fragment.instantiate(getApplicationContext(), fragmentName);
			Bundle arguments = new Bundle();
			arguments.putSerializable(ID, data);
			f.setArguments(arguments);
			mFragmentList.add(f);
			FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.add(R.id.frame_main, f);
			fragmentTransaction.commit();
		}
		drawer.closeDrawers();
	}

	public void switchContent(String fragmentName, String data, int indext) {
		Fragment f = null;
		boolean existed = false;
		if (mCurrentFragment != null) {
			if (!mCurrentFragment.getClass().getName().equals(fragmentName)) {

				for (int i = 0; i < mFragmentList.size(); i++) {
					if (mFragmentList.get(i).getClass().getName().equals(fragmentName)) {
						existed = true;
						f = mFragmentList.get(i);
						Bundle arguments = f.getArguments();
						arguments.putString(ID, data);
						arguments.putInt(INDEX, indext);
						FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
						fragmentTransaction.hide(mCurrentFragment).show(f);
						fragmentTransaction.commit();
						mCurrentFragment = f;
						break;
					}
				}
				if (!existed) {
					f = Fragment.instantiate(getApplicationContext(), fragmentName);
					Bundle arguments = new Bundle();
					arguments.putString(ID, data);
					arguments.putInt(INDEX, indext);
					f.setArguments(arguments);
					mFragmentList.add(f);
					FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
					fragmentTransaction.hide(mCurrentFragment).add(R.id.frame_main, f);
					fragmentTransaction.commit();
					mCurrentFragment = f;
				}
				TagName.showLogError(TAG, "SwitchContent data: exited:" + existed);
			}
		} else {
			f = Fragment.instantiate(getApplicationContext(), fragmentName);
			Bundle arguments = new Bundle();
			arguments.putString(ID, data);
			arguments.putInt(INDEX, indext);
			f.setArguments(arguments);
			mFragmentList.add(f);
			FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.add(R.id.frame_main, f);
			fragmentTransaction.commit();
		}
		drawer.closeDrawers();
	}

	public void clear() {
		if (mFragmentList.size() > 0) {
			for (int i = 0; i < mFragmentList.size(); i++) {
				getSupportFragmentManager().beginTransaction().remove(mFragmentList.get(i)).commit();
			}
			mFragmentList.clear();
		}
	}

//	@Override
//	protected void onDestroy() {
//		mFragmentList.clear();
//		mFragmentList = null;
//		super.onDestroy();
//	}
//
//
//	@Override
//	protected void onRestart() {
//		super.onRestart();
//		Log.i(TAG, "***** onRestart");
//	}
//
	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "***** onResume");
		// IP = getLocalIpAddress();
		// initPopulateFragment();
	}

	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						@SuppressWarnings("deprecation")
						String ip = Formatter.formatIpAddress(inetAddress.hashCode());
						return ip;
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(TAG, ex.toString());
		}
		return null;
	}
}
